#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int INTMAX = 2147483647;
const LL LLMAX = 9223372036854775807;
int n;

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    int x;

    while(T--){
        int max1 = 0, max2 = 0, min1 = 1e9 + 10, min2 = 1e9 + 10;
        cin >> n;
        
        for(int i = 1; i <= n; i ++){
            cin >> x;
            if(x > max1){
                max2 = max1;
                max1 = x;
            }else if(x == max1){
                max2 = max1;
            }else if(x > max2){
                max2 = x;
            }

            if(x < min1){
                min2 = min1;
                min1 = x;
            }else if(x == min1){
                min2 = min1;
            }else if(x < min2){
                min2 = x;
            }
        }

        cout << max1 + max2 - min1 - min2 << '\n';
    }


    return 0;
}